import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by IntelliJ IDEA.
 * User: User
 * Date: 20.08.12
 * Time: 9:41
 * To change this template use File | Settings | File Templates.
 */
public class FileActor {
    String[] filesNames;
    LinkedList<Node> files = new LinkedList<Node>();

    public FileActor(String[] filesNames) {
        this.filesNames = filesNames;
        try {
            reconnaissance();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    }

    private void reconnaissance() throws IOException {
        for (int i = 0; i < filesNames.length; i++) {
            File f = new File(filesNames[i]);
            // System.out.println("searching in file "+ f.getAbsolutePath());
            getKeys(f);
            // System.out.println("Keys getted");
        }

    }

    private void getKeys(File target) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(target));
        String line;
        Pattern p = Pattern.compile("===.*.===");
        Matcher m;
        while ((line = reader.readLine()) != null) {
            //System.out.println("working with line= " + line);
            m = p.matcher(line);
            //System.out.println("matching!!!!!   " + m.matches());
            if ((m.matches()) == true) {
                //  System.out.println("there!");
                String key = line.replaceAll("===", "");
                //System.out.println("creating node..");
                Node node = new Node(target, key);
                //      System.out.println("node created");
                //    System.out.println("target key=" + key);
                files.add(node);
            }
        }
        reader.close();
    }

    public ArrayList<String> get(String key) throws IOException {
        StringBuffer sb = new StringBuffer();
        sb.append("===");
        sb.append(key);
        sb.append("===");
        String line;
        String marker = sb.toString();
        ArrayList<String> section = new ArrayList<String>();
        File f = getFileFromNode(key);
        if (f == null) section.add("incorrect key");
        else {
            BufferedReader reader = new BufferedReader(new FileReader(f));
            while ((line = reader.readLine()) != null) {
                if (line.equals(marker)) {
                    line = "";
                    while ((line = reader.readLine()) != null) {
                        if ((keytest(line)) == false) {
                            section.add(line);
                        } else break;
                    }
                }
            }
            reader.close();
        }

        return section;
    }

    private File getFileFromNode(String key) {
        Iterator i = files.iterator();
        //System.out.println("Iterator getted and key =" + key);
        Node n;
        File file = null;
        while (i.hasNext()) {
            n = (Node) i.next();
            if ((n.getKey().equals(key)) == true) {
                file = n.getFile();
                //System.out.println(file.getAbsolutePath() + "its filepath");
            }
        }
        return file;
    }

    private boolean keytest(String str) {
        Pattern p = Pattern.compile("===.*.===");
        Matcher m;
        m = p.matcher(str);
        if ((m.matches()) == true) {
            return true;
        } else {
            return false;
        }
    }

}
